﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace bgp8.Controls
{
    public sealed partial class GameBoard : Canvas
    {

        #region Events
        //Add a new event type for the gameboard

        //A delegate dictates a signature for functions
        //We could have also reused the RoutedEventHandler which is a pre-defined delegate
        public delegate void GamePieceSelectionChangedHandler(Object sender, GamePieceEventArgs e);
        
        //We declare an event which holds functions that follow our delegate's signature
        //this is what we will add event handlers to using the += synxax
        //the event handlers must follow the signature described by our delegate.
        public event GamePieceSelectionChangedHandler GamePieceSelectionChanged; 

        //This is a simple wrapper for invoking our custom event.
        private void OnGamePieceSelectionChanged(GamePieceEventArgs e) 
        {
            GamePieceSelectionChanged(this, e);
        }
        #endregion

        public GameBoard()
        {
            this.InitializeComponent();
            this.Background = new SolidColorBrush(Colors.Transparent);
            this.IsTapEnabled = true;
            PointerPressed += GameBoard_PointerPressed;
        }

        public GameGrid GameBoardGrid { get; set; }

        private GamePiece _selectedGamePiece;

        public GamePiece SelectedGamePiece 
        { 
            get 
            {
                return _selectedGamePiece;
            }
            set 
            {
                GamePiece newPiece = (GamePiece)value;
                //Don't do anything if we don't have the given piece
                if (Children.Contains(newPiece)) 
                {
                    //don't do anything if it's the same piece
                    if (newPiece != SelectedGamePiece) 
                    {
                        DeselectAllChildren();
                        _selectedGamePiece = newPiece;
                        //this will trigger another GamePieceSelected event. It will be from the same piece so we will ignore it.
                        _selectedGamePiece.Selected = true;
                        OnGamePieceSelectionChanged(new GamePieceEventArgs(SelectedGamePiece));
                    }
                }
            }
        }

        private void DeselectAllChildren() 
        {
            foreach (GamePiece p in Children) 
            {
                p.Selected = false;
            }
        }


        public void AddGamePiece(GamePiece piece) 
        {
            piece.GamePieceSelected += OnGamePiecePressed;
            Children.Add(piece.Clone());
        }

        public void RemoveGamePiece(GamePiece piece) 
        {
            if (Children.Contains(piece)) 
            {
                piece.GamePieceSelected -= OnGamePiecePressed;
                Children.Remove(piece);
            }
        }

        private void OnGamePiecePressed(object sender, GamePieceEventArgs e)
        {
            this.SelectedGamePiece = e.Piece;
        }

        private void GameBoard_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            SelectedGamePiece = null;
        }

    }
}
